# import the fdc dataset --------------------------------------------------

df_fdc <- read.csv("datasets/data_fdc.csv")

# balance the data --------------------------------------------------------

treatment <- df_fdc$treatment

ivs <- cbind(df_fdc$recruitment_pool, df_fdc$treatment_nonterror,
             df_fdc$casualty_district, df_fdc$kurdish_district, 
             df_fdc$akp_district, df_fdc$higher_education, df_fdc$min_margin,
             df_fdc$turnout_jun)

eb_out <- ebalance(Treatment = treatment , X = ivs)

# create a data frame -----------------------------------------------------

df_table <- data.frame(variables = c("Recruitment Pool", "Non-terror Funeral",
                                     "Attack District", "Kurdish District", 
                                     "AKP District", "Higher Education",
                                     "Electoral Margin", "Turnout"),
                       treatment_bm = apply(ivs[treatment == 1, ], 2, mean),
                       control_bm = apply(ivs[treatment == 0, ], 2, mean),
                       treatment_am = apply(ivs[treatment == 1, ], 2, mean),
                       control_am = apply(ivs[treatment == 0, ], 2,
                                          weighted.mean, w = eb_out$w))

# print the results -------------------------------------------------------

kbl(df_table, digits = 2, format = "pipe",
    caption = "Treatment and control means, before and after entropy balancing",
    col.names = c("", "Treatment (Before)", "Control (Before)", "Treatment (After)", "Control (After)"))

# save the results --------------------------------------------------------

kbl(df_table, format = "latex", booktabs = TRUE, linesep = "", digits = 2,
    caption = "Treatment and control means, before and after entropy balancing",
    col.names = c("", "Treatment", "Control", "Treatment", "Control")) %>%
  add_header_above(c(" ", "Before Matching" = 2, "After Matching" = 2)) %>%
  cat(file = "tables/table_s4.tex")